<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="main_num boxdq">
          <div class="main_num_l box"></div>
          <el-button @click="show()" class="marginLeft10" type="primary"
            >新增</el-button
          >
        </div>
        <div class="main_table" style="min-height: 690px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column
              label="ID"
              prop="id"
              width="200"
              sortable
            ></el-table-column>
            <el-table-column
              label="名称"
              prop="name"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="操作"
              align="center"
              fixed="right"
              width="250"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <div @click="show(scope.row)">
                  <el-link type="primary">编辑</el-link>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>

    <el-dialog
      :title="this.info.id ? '编辑客户类型' : '新增客户类型'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="showForm"
      class="main_dialog dialog450"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="90px"
      >
        <el-form-item label="名称：" prop="name">
          <el-input
            v-model.trim="info.name"
            placeholder="类型名称"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(showForm = false), close()">取 消</el-button>
        <el-button type="primary" @click="addOrEdit" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, addType, editType } from "@/api/user/userType";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  components: {},
  data() {
    return {
      // 增加和编辑
      info: {
        name: ""
      },
      type: 0,
      id: 0,
      showForm: false,
      // 验证
      rules: {
        name: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ]
      },
      // 列表数据
      list: [],
      listLoading: false
    };
  },
  methods: {
    // 关闭清空表单
    close() {
      this.info = {
        name: ""
      };
      this.$refs["userListFrom"].clearValidate();
      this.showForm = false;
    },
    show(data) {
      if (data) {
        this.info = data;
      }
      this.showForm = true;
    },
    // 编辑保存
    addOrEdit() {
      this.$refs["userListFrom"].validate(valid => {
        if (valid) {
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name, this.info)) +
                  apiUrl().appsecret
              )
            },
            apiUrl().name,
            this.info
          );
          if (this.info.id) {
            editType(data).then(res => {
              if (res.errcode == 0) {
                this.showForm = false;
                this.$message({
                  message: "编辑成功",
                  type: "success"
                });
                this.getData();
              } else {
                this.$message.error(res.errmsg);
              }
            });
          } else {
            addType(data).then(res => {
              if (res.errcode == 0) {
                this.showForm = false;
                this.$message({
                  message: "新增成功",
                  type: "success"
                });
                this.getData();
              } else {
                this.$message.error(res.errmsg);
              }
            });
          }
        }
      });
    },
    // 列表
    getData() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          )
        },
        apiUrl().name
      );
      list(data).then(res => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.list = res.data.list.data;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    }
  },
  created() {
    // 列表
    this.getData();
  }
};
</script>
<style lang="scss" scoped>
.handle_btn {
  padding: 0;
  border: 0;
}
</style>
